package com.hyy.backend.controller;

import com.hyy.backend.common.Result;
import com.hyy.backend.dto.CommentDTO;
import com.hyy.backend.entity.Comment;
import com.hyy.backend.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    // 根据帖子 ID 获取评论列表
    @GetMapping("/post/{postId}")
    public Result<List<Comment>> getCommentsByPostId(@PathVariable Integer postId) {
        return Result.success(commentService.getCommentsByPostId(postId));
    }

    // 添加评论
    @PostMapping("/add")
    public Result<String> addComment(@RequestBody Comment comment) {
        boolean success = commentService.addComment(comment);
        return success ? Result.success("评论成功") : Result.error("评论失败");
    }

    // 删除评论（管理员或作者）
    @DeleteMapping("/{id}")
    public Result<String> deleteComment(@PathVariable Integer id) {
        boolean success = commentService.deleteComment(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }
}
